#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

#include <iostream>
#include <cstdlib>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
//#include <SUPERBIBLE/gltools.h>
#include <GL/gl.h>
#include <GL/glut.h>
#endif

using namespace std;

#include "Game.h"
#include "KeyManager.h"

void reshape(int w, int h);
long getTickCount();
void display(void);
void keyboard(unsigned char key, int x, int y);
void keyboardSpecial(int key, int x, int y);

// mouse
void mouse(int button, int state, int x, int y);
void mouseActiveMotion(int x, int y);
void mousePassiveMotion(int x, int y);

Game* game;
KeyManager* keyManager;

int main (int argc, char* argv[])
{
   
   game = new Game;  
   keyManager = new KeyManager(game);
   
   glutInit(&argc, argv);
	
	glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
   //glutInitDisplayMode(GLUT_SINGLE);
	
   glutInitWindowSize(700, 500);
	glutInitWindowPosition(10, 10);
	glutCreateWindow("3D Billiards");

	glutDisplayFunc(display);
	glutIdleFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
   glutSpecialFunc(keyboardSpecial);

   //adding here the mouse processing callbacks
	glutMouseFunc(mouse);
	glutMotionFunc(mouseActiveMotion);
	glutPassiveMotionFunc(mousePassiveMotion);
   
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);
   
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

   Config::getTextures()->load();
   glutMainLoop();
   if (game) delete game;
	
   return 0;
}

void reshape(int w, int h)
{
	game->reshape(w, h);
}

void display(void)
{
   game->draw();
}

void keyboard(unsigned char key, int x, int y)
{
   keyManager->keyChange(key, x, y);
}

void keyboardSpecial(int key, int x, int y)
{
  keyManager->specialKeyChange(key, x, y);
}

void mouse(int button, int state, int x, int y)
{
   keyManager->setButtonState(button, state, x, y);
   
   //cout << "Button: " << button << ", state: " << state << " (" << x << "," << y << ")\n";
}

void mouseActiveMotion(int x, int y)
{
   keyManager->setMousePosition(x, y);
}

void mousePassiveMotion(int x, int y)
{
   //cout << "Passive: (" << x << "," << y << ")\n";     
}

